package com.shaoshaohuo.app.adapter;

import java.util.List;
import com.shaoshaohuo.app.constant.CarOption;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.Order;
import com.shaoshaohuo.app.utils.StringHelper;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class OrderShipperListAdapter<T> extends MyBaseAdapter<Order> {
    private int type = 1;
	public OrderShipperListAdapter(Context ctx, List<Order> list, boolean flag) {
		super(ctx, list, flag);
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup group) {
		
		ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context,R.layout.view_order_list_shipper_item, null);
            holder = new ViewHolder();
            holder.startCityText = (TextView) convertView.findViewById(R.id.textview_start_city);
            holder.endCityText = (TextView) convertView.findViewById(R.id.textview_end_city);
            holder.priceText = (TextView) convertView.findViewById(R.id.textview_money);
            holder.weightText = (TextView) convertView.findViewById(R.id.textview_weight);
            holder.startAddressText = (TextView) convertView.findViewById(R.id.textview_start_address);
            holder.endAddressText = (TextView) convertView.findViewById(R.id.textview_end_address);
            holder.timeText = (TextView) convertView.findViewById(R.id.textview_time);
            holder.statusText = (TextView) convertView.findViewById(R.id.textview_status);
            holder.headerLayout = convertView.findViewById(R.id.layout_header);
            holder.carUserText = (TextView) convertView.findViewById(R.id.textview_car_user);
            holder.orderIdText = (TextView) convertView.findViewById(R.id.textview_order_id);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
		
        Order item = list.get(pos);
        
        holder.startCityText.setText(item.getOrigincityname());
        holder.endCityText.setText(item.getReceivecityname());
        holder.priceText.setText("￥"+item.getMoney());
//        holder.weightText.setText(item.getShipptype()+"/"+item.getShippweight());
        holder.startAddressText.setText(item.getOriginaddress());
        holder.endAddressText.setText(item.getReceiveaddress());
        holder.timeText.setText("下单时间:"+StringHelper.formatDateMinute(item.getCursor()));
        holder.statusText.setText(getStatusText(item.getStatus(), item.getCaruserstatus(), item.getShipperstatus(), item.getShipperdiscuss()));
        
        if ("1".equals(item.getShipptype())) {
            holder.weightText.setText(CarOption.cargoItems[0]);
        } else if ("2".equals(item.getShipptype())) {
            
            double parseDouble = Double.parseDouble(item.getShippweight());
            if(parseDouble>1000){
                holder.weightText.setText(CarOption.cargoItems[1] + "/" + (parseDouble/1000) + "吨");
            }else{
                holder.weightText.setText(CarOption.cargoItems[1] + "/" + item.getShippweight() + "kg");
            }
            
        } else if ("3".equals(item.getShipptype())) {
            holder.weightText.setText(CarOption.cargoItems[2] /*+ "/" + item.getShipparea() + "m³"*/);
        }else{
            holder.weightText.setVisibility(View.GONE);
        }
        
        if(TextUtils.isEmpty(item.getCaruserinfo().getRealname())){
            holder.carUserText.setText("");
        }else{
            holder.carUserText.setText("司机:"+item.getCaruserinfo().getRealname());
        }
        
        holder.orderIdText.setText("订单编号:"+item.getOrderid());
        
        switch (type) {
        case 1:
            holder.headerLayout.setVisibility(View.GONE);
            break;
        case 2:
            holder.headerLayout.setVisibility(View.VISIBLE);
            holder.carUserText.setVisibility(View.VISIBLE);
            holder.orderIdText.setVisibility(View.GONE);
            break;
        case 3:
            holder.headerLayout.setVisibility(View.VISIBLE);
            holder.carUserText.setVisibility(View.VISIBLE);
            holder.orderIdText.setVisibility(View.VISIBLE);
            break;
        }
        
		return convertView;
	}
	public void setType(int i) {
        this.type = i;
    }
	private String getStatusText(String status,String carStatus,String userStatus,String shipperdiscuss) {
		switch (Integer.parseInt(status)) {
		case -4:
		case -3:
		case -2:
		case -1:
			return "订单失效";
		case 1:
			if("2".equals(userStatus)){
				return "抢单中";
			}else if("3".equals(userStatus)){
				return "待支付";
			}
			break;
		case 2:
			if("1".equals(carStatus) && "3".equals(userStatus)){
				return "等待司机响应";
			}
			break;
		case 3:
			return "待支付";
		case 4:
			if("3".equals(userStatus)){
				return "待支付";
			}
			break;
		case 5:
		    if ("4".equals(userStatus)) {
		        return "待装货";
		    }else{
		        return "已装货";
		    }
		case 6:
		    if ("5".equals(userStatus)) {
		        return "运输中";
		    }else{
		        return "已送达";
		    }
		case 7:
			if("1".equals(shipperdiscuss)){
				return "已完成,未评价";
			}else{
				return "已完成,已评价";
			}
		default:
			break;
		}
		return "";
	}
	
	static class ViewHolder{
		TextView startCityText;
		TextView endCityText;
		TextView priceText;
		TextView weightText;
		
		TextView startAddressText;
		TextView endAddressText;
		TextView timeText;
		TextView statusText;
		
		View headerLayout;
        TextView carUserText;
        TextView orderIdText;
	}
	
}
